package ufabc.edu.helppo.gerador;

import java.util.List;
import java.io.IOException;
import java.security.MessageDigest;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;

import ufabc.edu.helppo.persistencia.HibernateUtil;
import ufabc.edu.helppo.persistencia.entidades.Usuario;

import java.util.ResourceBundle;
import ufabc.edu.helppo.rpc.singleton.HelppoProperties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import java.util.Properties;

public class Usuarios {

	public static List<Usuario> listaUsuarios() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<Usuario> lista = (List<Usuario>) session.createQuery(
				"FROM Usuario").list();
		session.close();
		return lista;
	}

	public static void apagarUsuario(String UUID) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		String hql = "from Usuario where UUID = '" + UUID + "'";
		Query query = session.createQuery(hql);
		Usuario user = (Usuario) query.uniqueResult();
		session.delete(user);
		tx.commit();
		session.close();
	}

	public static Usuario pegaUsuario(String UUID) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String hql = "from Usuario where UUID= '" + UUID + "'";
		Usuario user = (Usuario) session.createQuery(hql).uniqueResult();
		session.close();
		return user;
	}

	public static void salvaUsuario(Usuario user) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.update(user);

		tx.commit();
		session.close();
	}

	public static void enviarEmail(String UUID, String mensagem, String titulo) {
		enviarEmail(pegaUsuario(UUID), mensagem, titulo);
	}

	public static void enviarEmail(Usuario user, String mensagem, String titulo) {
		enviarEmail(user.getEmail(), mensagem, titulo);
	}

	public static void enviaEmail(String email, String mensagem, String titulo)
			throws MessagingException, IOException, Exception {

		ResourceBundle propertiesHelppo = HelppoProperties.getInstance();
		Message message = new MimeMessage(getSession());

		message.addRecipient(RecipientType.TO, new InternetAddress(email));
		message.addFrom(new InternetAddress[] { new InternetAddress(
				propertiesHelppo.getString("email_remetente")) });

		message.setSubject(titulo);
		message.setContent(mensagem, "text/plain");

		Transport.send(message);
	}

	private static javax.mail.Session getSession() throws Exception {
		Authenticator authenticator = new Authenticator();

		Properties properties = new Properties();
		properties.setProperty("mail.smtp.submitter", authenticator
				.getPasswordAuthentication().getUserName());
		properties.setProperty("mail.smtp.auth", "true");

		properties.setProperty("mail.smtp.host", "mail.ufabc.edu.br");
		properties.setProperty("mail.smtp.port", "25");

		return javax.mail.Session.getInstance(properties, authenticator);
	}

}

class Authenticator extends javax.mail.Authenticator {
	private PasswordAuthentication authentication;

	public Authenticator() throws Exception {
		ResourceBundle propertiesHelppo = HelppoProperties.getInstance();
		String username = propertiesHelppo.getString("email_remetente");
		String password = propertiesHelppo.getString("senha_remetente");
		authentication = new PasswordAuthentication(username, password);
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return authentication;
	}
}